package com.cowain.monitor.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cowain.monitor.dao.ErpSupplierDao;
import com.cowain.monitor.dao.PmsSupplierDao;
import com.cowain.monitor.datasources.DataSourceNames;
import com.cowain.monitor.datasources.annotation.DataSource;
import com.cowain.monitor.datasources.config.TransactionConfig;
import com.cowain.monitor.entity.ErpSoEntity;
import com.cowain.monitor.entity.ErpSupplierEntity;
import com.cowain.monitor.entity.PmsClientEntity;
import com.cowain.monitor.entity.PmsSupplierEntity;
import com.cowain.monitor.service.ErpSupplierService;
import com.cowain.monitor.utils.PmsConstantUtils;
import com.cowain.monitor.utils.PmsProjectUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  
 *  * @author 冬暖
 *  * @projectName pms-monitor
 *  * @title     ErpSupplierServiceImpl   
 *  * @package    com.cowain.monitor.service.impl  
 *  * @description    ERP供应商信息       
 *  * @date   2020/7/28 18:02  
 *  * @version V1.0.0
 *  
 */
@Service
public class ErpSupplierServiceImpl extends ServiceImpl<ErpSupplierDao, ErpSupplierEntity> implements ErpSupplierService {
    @Autowired
    private PmsSupplierDao supplierDao;
    @Autowired
    private PmsProjectUtil pmsProjectUtil;
    /**
     * 获取所有的供应商列表
     * @return 供应商
     */
    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public List<ErpSupplierEntity> findErpSupplierList(){
        return baseMapper.findErpSupplierList(PmsConstantUtils.IS_DEL);
    }

    /**
     * 添加供应商信息
     * @param supplierEntities 供应商列表
     */
    @Override
    @DataSource(name = DataSourceNames.FIRST)
    @Transactional(value = TransactionConfig.DEFAULT_TX,rollbackFor = Exception.class)
    public void insertSupplier(List<ErpSupplierEntity> supplierEntities){
        List<PmsSupplierEntity> infoList = new ArrayList<>();
        List<PmsSupplierEntity> infoEntityList = new ArrayList<>();
        supplierEntities.forEach(supplier -> {
            PmsSupplierEntity supplierEntity = new PmsSupplierEntity();
            Long clientNumber;
            if(supplier.getSupplierSiteEntity() != null && supplier.getSupplierSiteEntity().size() > 0){
                clientNumber = supplierDao.findErpSupplier(supplier.getSuppId(),supplier.getSupplierSiteEntity().get(0).getSuppAddress());
                supplierEntity.setSupplierAddress(supplier.getSupplierSiteEntity().get(0).getSuppAddress());
                supplierEntity.setSupplierContact(supplier.getSupplierSiteEntity().get(0).getSuppPhonePerson());
                supplierEntity.setContactPhone(supplier.getSupplierSiteEntity().get(0).getSuppPhoneNum());
            }else {
                clientNumber = supplierDao.findSupplier(supplier.getSuppId());
                supplierEntity.setSupplierAddress(null);
                supplierEntity.setSupplierContact(null);
                supplierEntity.setContactPhone(null);
            }
            supplierEntity.setSupplierCode(supplier.getSuppCode());
            supplierEntity.setSupplierName(supplier.getSuppName());
            supplierEntity.setInsertTime(new Date());
            supplierEntity.setId(supplier.getSuppId());
            supplierEntity.setInsertUser(null);
            if(clientNumber != null){
                infoEntityList.add(supplierEntity);
            }else {
                infoList.add(supplierEntity);
            }
        });
        List<PmsSupplierEntity> list1 = new ArrayList<>();
        boolean flag = false;
        List<Long> list = pmsProjectUtil.addSupperList(infoList, list1);
        if(infoList.size() > 0 && list.size() > 0){
            List<PmsSupplierEntity> list2 = new ArrayList<>();
            infoList.forEach(info -> {
                if(info.getSupplierCode() == null || StringUtils.isBlank(info.getSupplierCode())){
                    list2.add(info);
                }
            });
            infoList.removeAll(list2);
            if(infoList.size() > 0){
                List<List<PmsSupplierEntity>> split = pmsProjectUtil.split(infoList, 12);
                for (List<PmsSupplierEntity> pmsSupplierEntities : split) {
                    supplierDao.insertList(pmsSupplierEntities);
                }
                flag = true;
            }
        }
        if(infoEntityList.size() > 0){
            if(list1.size() > 0){
                infoEntityList.addAll(list1);
            }
            List<List<PmsSupplierEntity>> split = pmsProjectUtil.split(infoEntityList, 12);
            for (List<PmsSupplierEntity> pmsSupplierEntities : split) {
                supplierDao.updateInfoList(pmsSupplierEntities);
            }
        }
        if(list1.size() == 0){
            list1.addAll(infoEntityList);
        }
        if(list.size() > 0 || list1.size() > 0) {
            List<PmsSupplierEntity> supplierEntities1 = new ArrayList<>();
            if(flag){
                supplierEntities1.addAll(infoList);
            }
            pmsProjectUtil.addLog(supplierEntities1, list1, PmsConstantUtils.ERP_USER, PmsConstantUtils.ERP_USER);
        }
    }

    /**
     * 修改状态
     */
    @Override
    @DataSource(name = DataSourceNames.SECOND)
    @Transactional(value = TransactionConfig.SECOND_TX,rollbackFor = Exception.class)
    public void updateSupplier(List<ErpSupplierEntity> supplierEntities){
        supplierEntities.forEach(list -> {
            if(list.getSuppId() != null){
                list.setIsDel(1);
                if(list.getSupplierSiteEntity() != null && list.getSupplierSiteEntity().get(0).getSupplier() != null){
                    list.getSupplierSiteEntity().get(0).setIsDel(1);
                }
            }else {
                list.setIsDel(0);
                if(list.getSupplierSiteEntity() != null){
                    list.getSupplierSiteEntity().get(0).setIsDel(0);
                }
            }
        });
        List<ErpSupplierEntity> collect = supplierEntities.stream()
                .filter(s -> s.getIsDel() == 1)
                .collect(Collectors.toList());
        List<List<ErpSupplierEntity>> split = pmsProjectUtil.split(collect, 12);
        for (List<ErpSupplierEntity> erpSupplierEntities : split) {
            baseMapper.updateSupplier(erpSupplierEntities);
            erpSupplierEntities.forEach(es -> {
                if(es.getSupplierSiteEntity() != null && es.getSupplierSiteEntity().size() > 0){
                    baseMapper.updateSupplierSite(es.getSupplierSiteEntity());
                }
            });
        }
    }
}
